home *** CD-ROM | disk | FTP | other *** search
/ SCI Games E3 2005 Press Kit (USA) / SCI Games E3 2005 Press Kit (USA).bin / runme_mac.swf / scripts / __Packages / mx / controls / streamingmedia / RTMPPlayer.as < prev    next >
Text File  |  2005-05-05  |  12KB  |  420 lines

  1. class mx.controls.streamingmedia.RTMPPlayer extends mx.controls.streamingmedia.AbstractPlayer
  2. {
  3.    function RTMPPlayer(aMediaUrl, aMediaType, aVideoHolder, aTotalTime)
  4.    {
  5.       super();
  6.       if(aMediaUrl == null || aVideoHolder == null || aTotalTime == null)
  7.       {
  8.       }
  9.       this._mediaUrl = aMediaUrl;
  10.       this._mediaType = aMediaType;
  11.       this._videoHolder = aVideoHolder;
  12.       this._video = this._videoHolder._video;
  13.       this._totalTime = aTotalTime;
  14.       this._listeners = new Array();
  15.       this._connectTimeOutId = null;
  16.       this.init();
  17.    }
  18.    function init()
  19.    {
  20.       var _loc2_ = undefined;
  21.       _loc2_ = this._mediaUrl.indexOf(":");
  22.       if(_loc2_ == -1)
  23.       {
  24.       }
  25.       this._protocol = this._mediaUrl.substring(0,_loc2_).toLowerCase();
  26.       if(this._protocol != "rtmp" && this._protocol != "rtmps" && this._protocol != "rtmpt")
  27.       {
  28.       }
  29.       var _loc3_ = this._mediaUrl.substring(_loc2_ + 1,this._mediaUrl.length);
  30.       this._port = null;
  31.       var _loc5_ = _loc3_.indexOf(":");
  32.       if(_loc5_ != -1)
  33.       {
  34.          this._port = _loc3_.substring(_loc5_ + 1,_loc3_.length);
  35.          var _loc4_ = this._port.indexOf("/");
  36.          if(_loc4_ != -1 && _loc4_ < _loc5_)
  37.          {
  38.             this._port = this._port.substring(0,_loc4_);
  39.          }
  40.          else
  41.          {
  42.             this._port = null;
  43.          }
  44.       }
  45.       _loc2_ = _loc3_.lastIndexOf("/");
  46.       if(_loc2_ == -1)
  47.       {
  48.       }
  49.       this._appUrl = _loc3_.substring(0,_loc2_);
  50.       this._streamName = _loc3_.substring(_loc2_ + 1,_loc3_.length);
  51.       if(this._streamName.length == 0)
  52.       {
  53.       }
  54.       if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE)
  55.       {
  56.          _loc2_ = this._streamName.indexOf(".");
  57.          if(_loc2_ != -1)
  58.          {
  59.             var _loc6_ = undefined;
  60.             _loc6_ = this._streamName.substring(_loc2_).toLowerCase();
  61.             if(_loc6_ == ".flv")
  62.             {
  63.                this._streamName = this._streamName.substring(0,_loc2_);
  64.             }
  65.          }
  66.          this._streamName = "flv:" + this._streamName;
  67.       }
  68.       else if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.MP3_MEDIA_TYPE)
  69.       {
  70.          _loc2_ = this._streamName.indexOf(".");
  71.          if(_loc2_ != -1)
  72.          {
  73.             _loc6_ = this._streamName.substring(_loc2_).toLowerCase();
  74.             if(_loc6_ == ".mp3")
  75.             {
  76.                this._streamName = this._streamName.substring(0,_loc2_);
  77.             }
  78.          }
  79.          this._streamName = "mp3:" + this._streamName;
  80.       }
  81.       this.setPlaying(false);
  82.       this._isLoaded = false;
  83.       this._isLoading = false;
  84.       this._isPlayPending = false;
  85.       this._nc_rtmp = null;
  86.       this._nc_rtmpt = null;
  87.       this._playHeadPos = 0;
  88.       this.setSeeking(false);
  89.       this._isPausing = false;
  90.       this._sound = new Sound(this._videoHolder);
  91.       this.setVolume(mx.controls.streamingmedia.StreamingMediaConstants.DEFAULT_VOLUME);
  92.    }
  93.    function addListener(aListener)
  94.    {
  95.       this._listeners.push(aListener);
  96.    }
  97.    function removeAllListeners()
  98.    {
  99.       this._listeners.length = 0;
  100.    }
  101.    function broadcastEvent(status)
  102.    {
  103.       var _loc2_ = 0;
  104.       while(_loc2_ < this._listeners.length)
  105.       {
  106.          this._listeners[_loc2_].handlePlayer(this,status);
  107.          _loc2_ = _loc2_ + 1;
  108.       }
  109.    }
  110.    function bufferIsFull()
  111.    {
  112.       this.broadcastEvent("start");
  113.       if(!this.isPlaying())
  114.       {
  115.          this.pause();
  116.       }
  117.    }
  118.    function resizeVideo()
  119.    {
  120.       this.broadcastEvent("resizeVideo");
  121.       if(!this.isPlaying())
  122.       {
  123.          this.pause();
  124.       }
  125.    }
  126.    function toString()
  127.    {
  128.       return "RTMPPlayer: Playing " + this.getMediaUrl();
  129.    }
  130.    function close()
  131.    {
  132.       this._ns.onStatus = null;
  133.       this._ns.close();
  134.       this._ns = null;
  135.       if(this._nc_rtmp != null)
  136.       {
  137.          this._nc_rtmp.onStatus = null;
  138.          this._nc_rtmp.close();
  139.          this._nc_rtmp = null;
  140.       }
  141.       if(this._nc_rtmpt != null)
  142.       {
  143.          this._nc_rtmpt.onStatus = null;
  144.          this._nc_rtmpt.close();
  145.          this._nc_rtmpt = null;
  146.       }
  147.       this._video.clear();
  148.    }
  149.    function load()
  150.    {
  151.       this._isLoading = true;
  152.       this.actualConnect();
  153.       if(this._connectTimeOutId != null)
  154.       {
  155.          clearInterval(this._connectTimeOutId);
  156.          this._connectTimeOutId = null;
  157.       }
  158.       this._connectTimeOutId = setInterval(this,"onConnectTimeOut",60000);
  159.    }
  160.    function startStream(nc)
  161.    {
  162.       clearInterval(this._connectTimeOutId);
  163.       this._connectTimeOutId = null;
  164.       this._ns = new mx.controls.streamingmedia.PlayerNetStream(nc,this);
  165.       if(this._mediaType == mx.controls.streamingmedia.StreamingMediaConstants.FLV_MEDIA_TYPE)
  166.       {
  167.          this._video.attachVideo(this._ns);
  168.       }
  169.       this._video.attachVideo(this._ns);
  170.       this._videoHeight = this._video.height;
  171.       this._videoWidth = this._video.width;
  172.       this._ns.setBufferTime(5);
  173.       this._videoHolder.attachAudio(this._ns);
  174.       this._ns.play(this._streamName,0,-1);
  175.       this._isLoading = false;
  176.       this._isLoaded = true;
  177.       this._videoHolder._visible = false;
  178.       this.setPlaying(false);
  179.       if(this._isPlayPending)
  180.       {
  181.          this.play(null);
  182.       }
  183.       else
  184.       {
  185.          this._isPausing = false;
  186.          this.pause();
  187.       }
  188.    }
  189.    function onConnectTimeOut()
  190.    {
  191.       clearInterval(this._connectTimeOutId);
  192.       this._connectTimeOutId = null;
  193.       if(this._nc_rtmpt != null)
  194.       {
  195.          this._nc_rtmpt.onStatus = null;
  196.          this._nc_rtmpt.close();
  197.          this._nc_rtmpt = null;
  198.       }
  199.       if(this._nc_rtmp != null)
  200.       {
  201.          this._nc_rtmp.onStatus = null;
  202.          this._nc_rtmp.close();
  203.          this._nc_rtmp = null;
  204.       }
  205.       this._isLoading = false;
  206.       this._isLoaded = false;
  207.    }
  208.    function actualConnect()
  209.    {
  210.       if(this._protocol == "rtmp")
  211.       {
  212.          this._nc_rtmp = new mx.controls.streamingmedia.RTMPConnection(this);
  213.          this._nc_rtmp.onStatus = function(info)
  214.          {
  215.             if(info.code == "NetConnection.Connect.Success")
  216.             {
  217.                clearInterval(this._player._conn_Id);
  218.                this._nc_rtmpt.onStatus = null;
  219.                this._nc_rtmpt.close();
  220.                this._nc_rtmpt = null;
  221.                this._player.startStream(this);
  222.                this.popConnection();
  223.             }
  224.          };
  225.          this._nc_rtmp.connect("rtmp:" + this._appUrl,this._streamName);
  226.       }
  227.       if(this._protocol == "rtmpt" || this._protocol == "rtmp" && this._port == null)
  228.       {
  229.          this._nc_rtmpt = new mx.controls.streamingmedia.RTMPConnection(this);
  230.          this._nc_rtmpt.onStatus = function(info)
  231.          {
  232.             if(info.code == "NetConnection.Connect.Success")
  233.             {
  234.                this._nc_rtmp.onStatus = null;
  235.                this._nc_rtmp.close();
  236.                this._nc_rtmp = null;
  237.                this._player.startStream(this);
  238.                this.popConnection();
  239.             }
  240.          };
  241.          if(this._protocol == "rtmpt")
  242.          {
  243.             this._nc_rtmpt.connect("rtmpt:" + this._appUrl,this._streamName);
  244.          }
  245.          else
  246.          {
  247.             clearInterval(this._conn_Id);
  248.             this._conn_Id = setInterval(this,"connectRtmpt",3000);
  249.          }
  250.       }
  251.    }
  252.    function connectRtmpt()
  253.    {
  254.       clearInterval(this._conn_Id);
  255.       this._nc_rtmpt.connect("rtmpt:" + this._appUrl,this._streamName);
  256.    }
  257.    function play(startingPoint)
  258.    {
  259.       this.setPlaying(true);
  260.       if(startingPoint != null)
  261.       {
  262.          this._playHeadPos = startingPoint;
  263.       }
  264.       if(this._isLoading || !this._isLoaded)
  265.       {
  266.          this._isPlayPending = true;
  267.          if(!this._isLoaded)
  268.          {
  269.             this.load();
  270.          }
  271.          return undefined;
  272.       }
  273.       this._isPlayPending = false;
  274.       this._isPausing = false;
  275.       this._ns.pause(false);
  276.       this._ns.seek(this._playHeadPos);
  277.    }
  278.    function pause()
  279.    {
  280.       if(!this._isPausing)
  281.       {
  282.          this._ns.pause(true);
  283.          this._isPausing = true;
  284.          this._isPlayPending = false;
  285.          this._playHeadPos = this._ns.time;
  286.          this.setPlaying(false);
  287.       }
  288.    }
  289.    function stop()
  290.    {
  291.       this.pause();
  292.       this.setPlayheadTime(0);
  293.    }
  294.    function getPlayheadTime()
  295.    {
  296.       return this._ns.time;
  297.    }
  298.    function setPlayheadTime(position)
  299.    {
  300.       this._playHeadPos = position;
  301.       if(!this._isSeeking)
  302.       {
  303.          this._ns.seek(position);
  304.          this.setSeeking(true);
  305.       }
  306.       if(!mx.controls.streamingmedia.StreamingMediaConstants.SCRUBBING)
  307.       {
  308.          if(!this.isPlaying())
  309.          {
  310.             this._ns.pause(false);
  311.             clearInterval(this._momentaryPlayId);
  312.             this._momentaryPlayId = setInterval(this,"doneUpdateFrame",50);
  313.          }
  314.       }
  315.    }
  316.    function doneUpdateFrame()
  317.    {
  318.       clearInterval(this._momentaryPlayId);
  319.       this._momentaryPlayId = null;
  320.       this._ns.pause(true);
  321.    }
  322.    function playStopped()
  323.    {
  324.       this._statusCount = 2;
  325.       clearInterval(this._checkCompleteId);
  326.       this._checkCompleteId = setInterval(this,"checkComplete",50);
  327.    }
  328.    function checkComplete()
  329.    {
  330.       if(this._ns.bufferLength <= 0.001)
  331.       {
  332.          if(this._statusCount <= 0)
  333.          {
  334.             clearInterval(this._checkCompleteId);
  335.             this._checkCompleteId = null;
  336.             this.pause();
  337.             this.broadcastEvent("complete");
  338.          }
  339.          else
  340.          {
  341.             this._statusCount -= 1;
  342.          }
  343.       }
  344.    }
  345.    function getMediaUrl()
  346.    {
  347.       return this._mediaUrl;
  348.    }
  349.    function setMediaUrl(aUrl)
  350.    {
  351.       this._mediaUrl = aUrl;
  352.       this._isLoaded = false;
  353.       var _loc3_ = this.isPlaying();
  354.       var _loc2_ = this.getVolume();
  355.       this.close();
  356.       this.init();
  357.       this.setVolume(_loc2_);
  358.       if(_loc3_)
  359.       {
  360.          this.play(0);
  361.       }
  362.       else
  363.       {
  364.          this.load();
  365.       }
  366.    }
  367.    function getVolume()
  368.    {
  369.       return this._sound.getVolume();
  370.    }
  371.    function setVolume(aVol)
  372.    {
  373.       this._sound.setVolume(aVol);
  374.    }
  375.    function getMediaBytesLoaded()
  376.    {
  377.       return this._ns.bytesLoaded;
  378.    }
  379.    function getMediaBytesTotal()
  380.    {
  381.       return this._ns.bytesTotal;
  382.    }
  383.    function getTotalTime()
  384.    {
  385.       return this._totalTime;
  386.    }
  387.    function setTotalTime(aTime)
  388.    {
  389.       this._totalTime = aTime;
  390.    }
  391.    function mediaLoaded()
  392.    {
  393.    }
  394.    function logError(error)
  395.    {
  396.    }
  397.    function isSizeSet()
  398.    {
  399.       if(this._video.width > 0 && this._video.height > 0)
  400.       {
  401.          return true;
  402.       }
  403.       return false;
  404.    }
  405.    function isSizeChange()
  406.    {
  407.       if(this._video.width != this._videoWidth || this._video.height != this._videoHeight)
  408.       {
  409.          this._videoWidth = this._video.width;
  410.          this._videoHeight = this._video.height;
  411.          return true;
  412.       }
  413.       return false;
  414.    }
  415.    function setSeeking(isSeeking)
  416.    {
  417.       this._isSeeking = isSeeking;
  418.    }
  419. }
  420.